package cc.vicp.djx314.msing.web.music;

import java.io.File;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.beans.factory.annotation.Autowired;

import cc.vicp.djx314.msing.domain.config.MsingProperties;
import cc.vicp.djx314.msing.util.web.EncodeUtil;
import cc.vicp.djx314.msing.util.web.FileOutPutUtil;
import cc.vicp.djx314.msing.web.base.BaseAction;
import cc.vicp.djx314.msing.web.form.MusicFileForm;

public class MusicFileAction extends BaseAction {

	@Autowired
	MsingProperties msingProperties;

	@Autowired
	FileOutPutUtil fileOutPutUtil;

	@Autowired
	EncodeUtil encodeUtil;

	private File getFile(ActionForm form) {
		String path = ((MusicFileForm) form).getPath();
		String rootPath = msingProperties.getMusicRootPath();
		if (!(rootPath.endsWith("/") || rootPath.endsWith("\\"))) {
			rootPath = new StringBuilder(rootPath).append("/").toString();
		}
		String filePath = new StringBuilder(rootPath).append(path).toString();
		filePath = encodeUtil.getStringUTF_8(filePath);
		File file = new File(filePath);
		//logger.info("=={}==", filePath);
		return file;
	}

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
					throws Exception {
		fileOutPutUtil.fileOutPut(request, response, getFile(form));
		return null;
	}
}